var app = angular.module('demo',['ngRoute', 'appService']);

// 全局路由
app.config(['$routeProvider', function($routeProvider){

    $routeProvider
        .when('/',{template:'这是首页页面'})
        .when('/MibManage',{
            templateUrl:'manage.html',
            // controller: "MibManageCtrl"
        })
        .when('/NodeQuery',{
            templateUrl:'query.html'
        })
        .otherwise({redirectTo:'/'});
}]);

app.controller("IndexCtrl", ["$scope", "httpService",  function ($scope, httpService) {
    $scope.name = "joe";
}]);

app.controller("MibManageCtrl", ["$scope", "httpService", function ($scope, httpService, $http) {
    $scope.refresh = function(){
        httpService.get("/mib/getAllMibs", function (data) {
            $scope.moduleList = data;
        });
    };
    $scope.uploadMibFile = function(){
        var url = "http://127.0.0.1:8080/mib/uploadMibFile";
        var formData = new FormData();
        formData.append("mibFile",angular.element("#mibFile")[0].files[0]); //file.files[0] js的取值方式  取到页面的第一个type=”file“  "file"与MultipartFile file 名字一致
        httpService.upload(url, formData, function (data) {
            $scope.refresh();
        });
    };
    $scope.deleteModule = function(module){
        var data = {"id": module.id};
        httpService.post("/mib/deleteMibModule", data, false, function(data){
            $scope.refresh();
        })
    };
    $scope.refresh();
}]);

app.controller("MibQueryCtrl", ["$scope", "httpService", function ($scope, httpService) {
    $scope.MibTree = null;
    $scope.SelectedModule = null;
    $scope.MibModuleList = null;

    $scope.queryTree = function(){
        var param = {"ids": $scope.SelectedModule.id};
        httpService.get("/mib/getNodeTree?" + $.param(param, true), function (data) {
            var mibTree = $scope.MibTree = new ZTreeObject("MibTree", data);
            mibTree.InitSelectParentNotChildrenTree();
        })
    };
    $scope.queryTrees = function(){
        var list = [];
        $scope.MibModuleList.filter(function (module) {
             if(module.selected === true){
                 list.push(module.id);
             }
        });
        var param = {"ids": list};
        httpService.get("/mib/getNodeTree?" + $.param(param, true), function (data) {
            var mibTree = $scope.MibTree = new ZTreeObject("MibTree", data);
            mibTree.InitSelectParentNotChildrenTree();
        })
    };
    httpService.get("/mib/getAllMibs", function (data) {
        $scope.MibModuleList = data;
        $scope.SelectedModule = data[0];
        $scope.queryTree();
    })

}]);